﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SDM.ExamPlanner.BLL;
using SDM.ExamPlanner.DTO;

namespace sdm.exam.ui.ExamAdministration
{
    /// <summary>
    /// Interaction logic for TeacherForSubject.xaml
    /// </summary>
    public partial class TeacherForSubject : UserControl
    {
        Manager<Teacher> _teacherManager = new Manager<Teacher>();
        Manager<Studyplan> _courseManager = new Manager<Studyplan>();
        Manager<SchoolClass> _classManager = new Manager<SchoolClass>();

        Dictionary<int, string> _listDictionary = new Dictionary<int, string>();
        Dictionary<int, string> _teacherDictionary = new Dictionary<int, string>();
        Dictionary<int, string> _courseDictionary = new Dictionary<int, string>();
        Dictionary<int, string> _classDictionary = new Dictionary<int, string>(); 

        public TeacherForSubject()
        {
            InitializeComponent();
            populateComboBox();
            populateList();
        }

        private void populateList()
        {
            _listDictionary = _courseManager.Find().ToDictionary(item => item.Id,
                                                                 item => item.CoursePlans + " " + item.SchoolClass);
            _listDictionary = _teacherManager.Find().ToDictionary(item => item.Id,
                                                                  item => item.Firstname + " " + item.Lastename);
        }

        private void populateComboBox()
        {
            //Teacher
            _teacherDictionary = _teacherManager.Find().ToDictionary(item => item.Id,
                                                                     item => item.Firstname + " " + item.Lastename);
            cbbTeacher.DataContext = _teacherDictionary.Values;

            //Education
            _courseDictionary = _courseManager.Find().ToDictionary(item => item.Id, item => item.SchoolClass.Education.Name);
            cbbCourse.DataContext = _courseDictionary.Values;
            
            //Class
            _classDictionary = _classManager.Find().ToDictionary(item => item.Id, item => item.Name);
            cbbClass.DataContext = _classDictionary.Values;
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {

            KeyValuePair<int, string> teacherPair =
                _teacherDictionary.FirstOrDefault(t => t.Value.Equals(cbbTeacher.SelectedValue));
            KeyValuePair<int, string> coursePair =
                _courseDictionary.FirstOrDefault(t => t.Value.Equals(cbbCourse.SelectedValue));
            KeyValuePair<int, string> classPair =
                _classDictionary.FirstOrDefault(t => t.Value.Equals(cbbClass.SelectedValue));
            List<Teacher> lstTeacher = new List<Teacher>();
            Teacher teacher = _teacherManager.Find().FirstOrDefault(item => item.Id == teacherPair.Key);
            lstTeacher.Add(teacher);
            Studyplan course = _courseManager.Find().FirstOrDefault(item => item.Id == coursePair.Key);
            SchoolClass schoolClass = _classManager.Find().FirstOrDefault(item => item.Id == classPair.Key);
            var changes = new SchoolClass()
                              {
                                  Teachers =lstTeacher,
                                  Name = schoolClass.ToString(),
                              };
            _classManager.Save(changes);
            _courseManager.Save(course);
        }
    }
}
